package cn.shiwei.shopcart.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.shopcart.service.IShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShopCartController {

    @Autowired
    private IShopCartService shopCartService;

    @GetMapping("/get") // get获取购物车列表演示, ok, 只需要哪几项(参数)所以其他的没加上
    public RequestStatus getCart() {
        try {
            return shopCartService.getCart();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PutMapping("/add/{id}") // 添加add 演示, 添加成功
    public RequestStatus addCart(@PathVariable Long id) {
        try {
            System.out.println("添加购物车:" + id);
            shopCartService.addCart(id);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @DeleteMapping("/del/{id}") // 删除演示, 传入的shopcartid, 删除不是删除, 而是改变状态, 删除ok
    public RequestStatus delCart(@PathVariable Long id) {
        try {
            System.out.println("删除购物车:" + id);
            shopCartService.delCart(id);
            return RequestStatus.me();
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @GetMapping("/cal") // 获取选中向 的总金额, 1,2 总金额210(包括邮费), 获取成功
    public RequestStatus calCart(@RequestBody List<Long> ids) {
        try {
            System.out.println("购物车金额计算:" + ids);
            return RequestStatus.me().setResultObj(shopCartService.calCart(ids));
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }
}
